-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move common pieces of mock REST servers to a new package #267
Conversation
Package restserver implements the building blocks to create similar mock servers that talk REST. Application type holds some global config and the cobra Cmds. ServerBase contains most of what a Server implementation will need. It requires: - a function that provides the net address - a configured ServeMux It offers: - serve - stop - validation boilerplate (the method was previously called `handle`, now `ValidateRequest` :) ) Type-erasing `Settings` (via interface) was necessary, because individual servers will have their own needs. (PS: I'm keen to ideas of how to remove the dependency on GetAddress and SetAddress)
I managed to sneak a little bit of OOP into Go :) contractsmockserver main business now is its own initialization which requires creating a ServeMux and handling the requests that manage to pass through validation. Boilerplate was "inherited" from ServerBase.
Much shorter :)
#268 shows how close those servers are :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very, very nice stuff. Really cool how small the server specialization ends up being, both here and in #268.
Maybe we could simplify some things with generics to avoid the factories, but the effort may not be worth the savings.
mocks/contractserver/contractsmockserver/contractsmockserver.go
Outdated
Show resolved
Hide resolved
and the receiver becomes "app" instead of simply "a"
That's the only place where that method is useful.
Address can only be set via Serve() Serve() accepts empty string for address, defaulting to "localhost:0" Initialization of Server implementations become cleaner No need to leak the address getters/setters to other levels. All handled within the restserver package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some changes, this is looking nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
The details of the responses may change soon, but the most important thing about this PR is that it puts in place the code we need to run a REST server that will mock the MS Store API. It reuses the bits extracted from the contracts server mock in #267 and reimplements the relevant parts to create the following endpoints: `/allauthenticatedusers` - returns some representation of the user accounts found locally authenticated on the system. `/generateuserjwt` - returns the user JWT if the query parameters check and the settings allow. `/getproducts` - returns a collection of products - JSON content type. `/purchase` - handles a purchase request, updating the in-memory server state if the transaction is accepted. That should be enough for us to implement the client API and some test cases & fixtures soon.
The store mock server is structurally very close to the contracts mock: YAML configuration, similar CLI, both talk via REST, both may have blocked or disabled endpoints etc.
Thus I though that I could move the guts of the contracts mock into a new package and make both mock servers inherit from this common base.
No semantics or endpoint behaviors should have changed with this.